package com.example.smartcommunity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smartcommunity.com.example.utils.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends AppCompatActivity {
    private Button login;//登录按钮
    private EditText username;
    private EditText password;//编辑框
    private TextView tv_register, tv_find_psw;//立即注册，找回密码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        tv_register = findViewById(R.id.tv_register);
        tv_find_psw = findViewById(R.id.tv_find_psw);
        login = findViewById(R.id.login);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);

        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisteredActivity.class);
                startActivity(intent);
            }
        });
        tv_find_psw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RetrieveActivity.class);
                startActivity(intent);
            }
        });


        //登录按钮的点击事件
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取用户名和密码
                String usernameStr = username.getText().toString().trim();
                String passwordStr = password.getText().toString().trim();
                //判断用户名和密码
                if (TextUtils.isEmpty(usernameStr)) {
                    Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(passwordStr)){
                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                } else {
                    Map<String, Object> map = new HashMap<>();
                    OkHttpUtils.getDataFromIntent("login?username=" + usernameStr + "&password=" + passwordStr, map, new OkHttpUtils.CallBack() {
                        @Override
                        public void successful(String string) {
                            try {
                                JSONObject jsonObject = new JSONObject(string);
                                String msg = jsonObject.getString("msg");
                                if (msg.equals("用户名密码错误")) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(getApplicationContext(), "用户名密码错误", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                } else {
                                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            LoginActivity.this.finish();

                        }
                        @Override
                        public void onErrer(Exception e) {
                            Log.e("请求错误", e.getMessage());
                        }
                    });
                }

            }
        });

    }
}
